package com.jzcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzcn.common.base.BaseServiceImpl;
import com.jzcn.common.dto.common.KeyWordDTO;
import com.jzcn.common.dto.enterprise.OrgRecordDTO;
import com.jzcn.common.dto.system.position.PositionRecordDTO;
import com.jzcn.common.enums.BaseStatusEnum;
import com.jzcn.common.enums.DeleteEnum;
import com.jzcn.common.exceptions.BusinessException;
import com.jzcn.common.utils.BeanUtils;
import com.jzcn.common.utils.JzPage;
import com.jzcn.domain.EntOrg;
import com.jzcn.domain.EntPosition;
import com.jzcn.mapper.EntPositionMapper;
import com.jzcn.service.EntPositionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class EntPositionServiceImpl extends BaseServiceImpl<EntPositionMapper, EntPosition> implements EntPositionService {

    @Override
    public JzPage<PositionRecordDTO> listPage(KeyWordDTO dto) {
        IPage<EntPosition> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<EntPosition> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(dto.getKeyword())){
            wrapper.like(EntPosition::getName,dto.getKeyword());
        }
        if(dto.getStatus()!=null){
            wrapper.eq(EntPosition::getStatus,dto.getStatus());
        }
        wrapper.orderByAsc(EntPosition::getCreateTime);
        return BeanUtils.convertPage(new JzPage<>(baseMapper.selectPage(iPage,wrapper)), PositionRecordDTO.class);
    }

    @Override
    public Boolean create(PositionRecordDTO dto) {
        if(super.checkUniqueness("name",dto.getName())){
            throw new BusinessException("职位名称已存在");
        }
        dto.setCreateTime(new Date());
        EntPosition sysPosition = new EntPosition();
        BeanUtils.copyProperties(dto,sysPosition);
        return super.save(sysPosition);
    }

    @Override
    public Boolean update(PositionRecordDTO dto) {
        dto.setUpdateTime(new Date());
        EntPosition sysPosition = new EntPosition();
        BeanUtils.copyProperties(dto,sysPosition);
        return super.update(sysPosition);
    }

    @Override
    public boolean delete(Integer id) {
        return super.remove(id);
    }


    @Override
    public List<PositionRecordDTO> posEmployeeList(){
        LambdaQueryWrapper<EntPosition> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EntPosition::getIsDelete, DeleteEnum.OPEN_DELETE.getValue());
        wrapper.eq(EntPosition::getStatus, BaseStatusEnum.START.getValue());
        return BeanUtils.convertList(baseMapper.selectList(wrapper), PositionRecordDTO.class);
    }
}
